package com.flight.dao;

import com.flight.bean.AirInfo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AirInfoDaoImpl extends BaseDao implements AirInfoDao {
    @Override
    public List<AirInfo> getAirInfoAll() {
        AirInfo air = null;
        try {

            String sql = "select * from airinfo";
            List<AirInfo> list = new ArrayList<>();
            ResultSet rs = query(sql, null);
            while(rs.next()){
                air = new AirInfo();
                air.setId(rs.getInt("id"));
                air.setAirId(rs.getString("airid"));
                air.setAddress(rs.getString("address"));
                air.setAirDate(rs.getDate("airdate"));
                list.add(air);

            }
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return null;
    }

    @Override
    public List<AirInfo> getByAirDate(String date) {
        ArrayList resList = new ArrayList();
        try {
            String sql = "select * from airinfo where airdate=?";
            ArrayList list = new ArrayList();
            list.add(date);
            ResultSet rs = query(sql, list);
            while(rs.next()){
                AirInfo air = new AirInfo();
                air.setId(rs.getInt("id"));
                air.setAirId(rs.getString("airid"));
                air.setAddress(rs.getString("address"));
                air.setAirDate(rs.getDate("airdate"));
                resList.add(air);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return resList;
    }

    @Override
    public List<AirInfo> getByAddress(String address) {
        ArrayList resList = new ArrayList();
        try {
            String sql = "select * from airinfo where address like ?";
            ArrayList list = new ArrayList();
            list.add("%"+address+"%");
            ResultSet rs = query(sql, list);
            while(rs.next()){
                AirInfo air = new AirInfo();
                air.setId(rs.getInt("id"));
                air.setAirId(rs.getString("airid"));
                air.setAddress(rs.getString("address"));
                air.setAirDate(rs.getDate("airdate"));
                resList.add(air);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return resList;
    }


    @Override
    public int deleteAirInfo(String did) {
        AirInfo air = null;
        try {
            air = new AirInfo();
            String sql = "delete from airinfo where airid=?";
            ArrayList list = new ArrayList();
            list.add(did);
            k = update(sql, list);
        }finally {
            closeAll();
        }
        return k;
    }

    @Override
    public int updateAirInfo(AirInfo air,String oldId) {
        try {
            String sql = "update airinfo set airid=?,address=?,airdate=? where airid=?";
            ArrayList param = new ArrayList();
            param.add(air.getAirId());
            param.add(air.getAddress());
            param.add(air.getTime());
            param.add(oldId);
            k = update(sql, param);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return k;
    }

    @Override
    public AirInfo findByAirId(String airId) {
        AirInfo air = null;
        try {
            String sql = "select * from airinfo where airid=?";
            ArrayList list = new ArrayList();
            list.add(airId);
            ResultSet rs = query(sql, list);
            while(rs.next()){
                air = new AirInfo();
                air.setId(rs.getInt("id"));
                air.setAirId(rs.getString("airid"));
                air.setAddress(rs.getString("address"));
                air.setAirDate(rs.getDate("airdate"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return air;
    }


}
